package com.ht.mt.sric.managed.gestion.tramo;

import com.ht.mt.sric.dto.TramoDto;

import com.ht.mt.sric.entity.Tramo;
import com.ht.mt.sric.sesion.remota.TramoSessionEJBBeanRemote;
import com.ht.mt.sric.util.ServiceLocator;

import com.ht.mt.sric.util.UConstantes;
import com.ht.mt.sric.util.UParametros;
import com.ht.mt.sric.util.UProperties;


import com.ht.mt.sric.utiles.FacesUtil;

import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;


import javax.faces.application.ViewHandler;
import javax.faces.bean.ManagedBean;

import javax.faces.bean.RequestScoped;


import javax.faces.bean.ViewScoped;
import javax.faces.component.UIViewRoot;

import javax.faces.context.FacesContext;

import oracle.adf.view.rich.component.rich.data.RichTable;

import oracle.adf.view.rich.component.rich.nav.RichButton;

import oracle.adf.view.rich.context.AdfFacesContext;

import org.apache.myfaces.trinidad.event.ReturnEvent;

@ManagedBean(name = "tramoMB")
@RequestScoped
public class GestionTramoMB {

    private ServiceLocator sLocator;
    private TramoSessionEJBBeanRemote ejbTramo;
    private List<TramoDto> lstTramos;
    private Integer typeWindows;
  
    private RichTable tbTramo;

    private TramoDto tramo;

    private UIViewRoot dialog;
    private ViewHandler viewHandler;
    private AdfFacesContext adfContext;
    private HashMap windowProperties;
    private HashMap dialogParameters;
    private FacesContext facesContext;
    private RichButton btnNewTramo;
    private RichButton btnUpdateTramo;
    private RichButton btnViewTramo;


    public GestionTramoMB() {
        try {


            sLocator = new ServiceLocator();
            ejbTramo = (TramoSessionEJBBeanRemote) sLocator.findServiceRemote(UProperties.getMessage(UParametros.RUTA_JNDI, "JNDI_TRAMO"));
            tbTramo = new RichTable();
            lstTramos = TramoDto.getListTramoDto(ejbTramo.getTramoFindAll());
            if (lstTramos == null) {
                lstTramos = new ArrayList<>();
            }

            windowProperties = new HashMap();
            dialogParameters = new HashMap();
            facesContext = FacesContext.getCurrentInstance();
            adfContext = FacesUtil.getAdfFacesContext();
            viewHandler = facesContext.getApplication().getViewHandler();
            
          

        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    public String newTramo() {
        try {
            tramo = new TramoDto();
           
            typeWindows = UConstantes.ADD;
            dialog = viewHandler.createView(facesContext, "/faces/gestion/tramo/au_tramo.jspx");
            windowProperties.put("width", new Integer(300));
            windowProperties.put("height", new Integer(200));
            dialogParameters.put("tramo", tramo);
            dialogParameters.put("typeWindows", typeWindows);
            adfContext.launchDialog(dialog, dialogParameters, btnNewTramo, true, windowProperties);


        } catch (Exception e) {
            e.printStackTrace();

        }
        return "";
    }


    public String updateTramo() {
        try {
            tramo = new TramoDto();
            System.out.println("Tb::" + tbTramo);
            System.out.println("tramo antes::" + tramo);
            if ((TramoDto) tbTramo.getSelectedRowData() == null) {
                System.out.println("TramoDto::" + tramo);
            } else {
               
                typeWindows = UConstantes.UPDATE;
                tramo = (TramoDto) tbTramo.getSelectedRowData();
                dialog = viewHandler.createView(facesContext, "/faces/gestion/tramo/au_tramo.jspx");
                windowProperties.put("width", new Integer(300));
                windowProperties.put("height", new Integer(200));
                dialogParameters.put("tramo", tramo);
                dialogParameters.put("typeWindows", typeWindows);
                adfContext.launchDialog(dialog, dialogParameters, btnUpdateTramo, true, windowProperties);
            }

        } catch (Exception e) {
            e.printStackTrace();

        }

        return "";
    }
    
    public String viewTramo() {
        try {
            tramo = new TramoDto();
            System.out.println("Tb::" + tbTramo);
            System.out.println("tramo antes::" + tramo);
            if ((TramoDto) tbTramo.getSelectedRowData() == null) {
                System.out.println("TramoDto::" + tramo);
            } else {
               
                typeWindows = UConstantes.VIEW;
                tramo = (TramoDto) tbTramo.getSelectedRowData();
                dialog = viewHandler.createView(facesContext, "/faces/gestion/tramo/au_tramo.jspx");
                windowProperties.put("width", new Integer(300));
                windowProperties.put("height", new Integer(200));
                dialogParameters.put("tramo", tramo);
                dialogParameters.put("typeWindows", typeWindows);
                adfContext.launchDialog(dialog, dialogParameters, btnViewTramo, true, windowProperties);
            }

        } catch (Exception e) {
            e.printStackTrace();

        }

        return "";
    }

    public String deleteTramo() {
        return "";
    }

  

    public void loadTable(ReturnEvent returnEvent) {
    }

    public void setLstTramos(List<TramoDto> lstTramos) {
        this.lstTramos = lstTramos;
    }

    public List<TramoDto> getLstTramos() {
        return lstTramos;
    }


  

    public void setTramo(TramoDto tramo) {
        this.tramo = tramo;
    }

    public TramoDto getTramo() {
        return tramo;
    }


    public void setTbTramo(RichTable tbTramo) {
        this.tbTramo = tbTramo;
    }

    public RichTable getTbTramo() {
        return tbTramo;
    }


    public void setBtnNewTramo(RichButton btnNewTramo) {
        this.btnNewTramo = btnNewTramo;
    }

    public RichButton getBtnNewTramo() {
        return btnNewTramo;
    }

    public void setBtnUpdateTramo(RichButton btnUpdateTramo) {
        this.btnUpdateTramo = btnUpdateTramo;
    }

    public RichButton getBtnUpdateTramo() {
        return btnUpdateTramo;
    }


    public void setBtnViewTramo(RichButton btnViewTramo) {
        this.btnViewTramo = btnViewTramo;
    }

    public RichButton getBtnViewTramo() {
        return btnViewTramo;
    }

}
